package com.woniu40.petai_manager.web;

import com.github.pagehelper.PageInfo;
import com.woniu40.petai_manager.entity.User;
import com.woniu40.petai_manager.result.ResponseResult;
import com.woniu40.petai_manager.service.UserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@CrossOrigin
@RequestMapping("/user")
@Api(value = "用户管理")
public class UserController {

    @Autowired
    UserService userService;

    @GetMapping("/findUsers")
    @ApiOperation(value = "查询用户列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name ="name",required = false,dataType = "String"),
            @ApiImplicitParam(name ="pageNum",required = true,dataType = "Integer"),
            @ApiImplicitParam(name ="pageSize",required = true,dataType = "Integer")
    })
    public ResponseResult<PageInfo<User>> findUsers(String name,@RequestParam(defaultValue = "1") Integer pageNum,@RequestParam(defaultValue = "5") Integer pageSize){
        PageInfo<User> users = userService.findUsers(pageNum,pageSize,name);
        ResponseResult<PageInfo<User>> result = new ResponseResult<>();
        result.setCode(200);
        result.setMessage("查询成功");
        result.setData(users);
        return result;
    }


    @PutMapping("/update")
    @ApiOperation(value = "更改用户状态")
    @ApiImplicitParam(name = "name",required = true,dataType = "Integer")
    public ResponseResult<String> update(Integer userid){
        int update = userService.update(userid);
        ResponseResult<String> result = new ResponseResult<>();
        if(update!=0){
            result.setCode(200);
            result.setMessage("更新成功");
        }else {
            result.setCode(108);
            result.setMessage("更新失败");
        }
        return result;
    }




}
